<?php

namespace App\B2BProject\Custom\Business;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Store extends Model
{
    protected $guarded = ['member_id'];

    /**
     * 店铺的对应的管理者
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function member()
    {
        return $this->belongsTo('App\B2BProject\Custom\Provider', 'member_id', 'id');
    }

    /**
     * 店铺主要经营的品牌
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function mainBrand()
    {
        return $this->belongsToMany('App\B2BProject\Custom\Business\Brand');
    }

    /**
     * 店铺所属的主要分类，该分类是网站服务方指定的，用于标示供应商的服务类型
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function mainCategory()
    {
        return $this->belongsToMany('App\B2BProject\Custom\Business\GoodsCategory', 'category_store');
    }

    /**
     * 获取关联的授权书
     */
    public function authorizations()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\ProviderAuthorization', 'store_id', 'id');
    }

    /**
     * 获取该店铺下的分类
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function categories()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\GoodsCategory', 'store_id', 'id');
    }

    /**
     * 获取当前店铺下的商品列表
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function goods()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\Goods', 'store_id', 'id');
    }

    /**
     * 价格策略
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function priceStrategy()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\PriceStrategy', 'store_id', 'id');
    }

    /**
     * 查询存在 LOGO 的店铺
     *
     * @param Builder $query
     *
     * @return $this
     */
    public function scopeWithLogo(Builder $query)
    {
        return $query->where('store_logo', '!=', '');
    }

    /**
     * 判断当前用户是否是供采分销关系户
     */
    public function isGranted()
    {
        return $this->userIsGranted(\Auth::id());
    }

    /**
     * 查看指定用户是否具有授权标识
     *
     * @param $user
     *
     * @return bool
     */
    public function userIsGranted($user)
    {
        $authorizations = $this->getAuthorization($user);

        return !is_null($authorizations) ?: false;
    }

    /**
     * 获取当前店铺授权信息表
     *
     * @param $user
     *
     * @return mixed
     */
    public function getAuthorization($user)
    {
        $authorization = $this->member->authorizationRequests()->available($user)->first();

        return $authorization;
    }

    /**
     * 获取当前店铺下的订单列表
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function order()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\Order', 'store_id', 'id');
    }

    /**
     * 该店铺下的导航
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function nav()
    {
        return $this->hasMany('App\B2BProject\Custom\StoreNavigation', 'store_id', 'id');
    }
}
